package com.xiaoka.flutter_plugin_gdmap.AMapNavi;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.amap.api.maps.AMapException;
import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.AMapNaviViewOptions;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.enums.PathPlanningStrategy;
import com.amap.api.navi.model.AMapCalcRouteResult;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviRouteNotifyData;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.google.gson.Gson;
import com.xiaoka.flutter_plugin_gdmap.Constant;
import com.xiaoka.flutter_plugin_gdmap.LittleUtils;

import java.util.ArrayList;
import java.util.List;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;


/**
 * Copyright (C), 2020 - 2999, Sichuan Xiaoka Technology Co., Ltd.
 *
 * @Description:导航控件
 * @Author: lch
 * @Date: 2020/11/12 15:23
 **/
public class AMapNaviPluginView implements PlatformView, MethodChannel.MethodCallHandler, AMapNaviListener, AMapNaviViewListener {

    private final Activity mActivity;
    private final AMapNaviBean aMapNaviBean;

    private MethodChannel mChannel;

    private AMapNaviView mAMapNaviView;//导航视图类
    private AMapNavi mAMapNavi;//导航控制类

    private NaviLatLng mStartLatlng;
    private NaviLatLng mEndLatlng;

    private List<NaviLatLng> startList = new ArrayList<>();
    private List<NaviLatLng> endList = new ArrayList<>();
    private List<NaviLatLng> newEndList = new ArrayList<>();

    public AMapNaviPluginView(BinaryMessenger binaryMessenger, Activity activity, int viewId, Context context, AMapNaviBean aMapNaviBean) throws AMapException {
        mActivity = activity;
        this.aMapNaviBean = aMapNaviBean;
        mChannel = new MethodChannel(binaryMessenger, Constant.AMAP_CHANNEL_NAME + "/amap_navi" + viewId);
        mChannel.setMethodCallHandler(this);

        AMapNaviViewOptions options = initializeOptions();
        mAMapNaviView = new AMapNaviView(mActivity, options);
        mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE);//设置车头朝向  0-CAR_UP_MODE 车头朝上模式  1-NORTH_UP_MODE 正北朝上模式
        mAMapNaviView.onCreate(null);
        mAMapNaviView.setAMapNaviViewListener(this);
        mAMapNaviView.getMap();
        mAMapNavi = AMapNavi.getInstance(mActivity.getApplicationContext());
        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.setUseInnerVoice(true);//默认开启内置语音
        mAMapNavi.setMultipleRouteNaviMode(true);//设置多路线导航模式(导航中拥有若干条备选路线供用户选择), 或单路线导航模式(默认模式).
        mAMapNavi.setControlMusicVolumeMode(1);//设置导航播报时压低音乐、暂停音乐，只有在使用内置语音时生效（Android8.0以下系统不支持暂停音乐，设置暂停音乐无效）
    }

    private AMapNaviViewOptions initializeOptions() {
        //这里可以通过参数进行自定义
        AMapNaviViewOptions options = new AMapNaviViewOptions();
        options.setAfterRouteAutoGray(true);//通过路线是否自动置灰
//        options.setPointToCenter(0.5, 0.7);
        options.setAutoChangeZoom(true);//设置是否开启动态比例尺 (锁车态下自动进行地图缩放变化) true,自动改变；false，不自动改变
        options.setScreenAlwaysBright(true);//开启常亮
        options.setAutoLockCar(true);//设置自动锁车逻辑开关 true代表自动锁车，false代表不自动锁车
        options.setCameraBubbleShow(true);//设置路线上的摄像头气泡是否显示
        options.setRealCrossDisplayShow(true);//设置是否显示路口放大图(实景图)
//        options.setModeCrossDisplayShow(true);//设置是否显示路口放大图(路口模型图)
        options.setTrafficInfoUpdateEnabled(aMapNaviBean.isTrafficInfoUpdateEnabled());//开启交通播报
        options.setTrafficLine(true);//设置地图上是否显示交通路况（彩虹线）
        options.setTrafficLayerEnabled(true);//实时交通按钮
        options.setTrafficBarEnabled(true);//显示路况光柱
        options.setNaviArrowVisible(true);//设置路线转向箭头隐藏和显示

//        options.setRouteListButtonShow(false);//显示全览按钮
//        options.setSettingMenuEnabled(false);//设置菜单按钮是否在导航界面显示。

        options.setRouteListButtonShow(true);//显示全览按钮
        options.setSettingMenuEnabled(false);//设置菜单按钮是否在导航界面显示。
        return options;
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        Log.e("导航~~~~~~~~~~", "插件接收到通信Method:" + call.method);
        if (call.method.equals("navigation")) {
            Log.e("导航~~~~~~~~~~", "收到的数据.终点： " + call.argument("endLatlng").toString());
            mStartLatlng = new Gson().fromJson(call.argument("location").toString(), NaviLatLng.class);
            mEndLatlng = new Gson().fromJson(call.argument("endLatlng").toString(), NaviLatLng.class);
            startList.clear();
            endList.clear();
            startList.add(mStartLatlng);
            endList.add(mEndLatlng);
            //目前采用驾车路径规划的计算策略10(速度优先+躲避拥堵+距离较短）),以前用的策略2(单路径-距离优先（距离最短）)
            mAMapNavi.calculateDriveRoute(startList, endList, null, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);
            mAMapNavi.startNavi(NaviType.GPS);//驾车导航
            result.success(true);
        } else if (call.method.equals("stopNavi")) {
            Log.e("导航~~~~~~~~~~", "收到stopNavi");
            mAMapNavi.destroy();
        }
    }

    @Override
    public View getView() {
        return mAMapNaviView;
    }

    @Override
    public void dispose() {
        if (mAMapNaviView != null) {
            mAMapNaviView.setAMapNaviViewListener(null);
            mAMapNaviView.onDestroy();
        }
        if (mAMapNavi != null) {
            mAMapNavi.removeAMapNaviListener(this);
            mAMapNavi.stopNavi();
            mAMapNavi.destroy();
        }
        if (mChannel != null) {
            mChannel.setMethodCallHandler(null);
        }
    }

    @Override
    public void onInitNaviFailure() {
        Log.e("导航～～～～～～", "初始化导航失败 ");
    }

    @Override
    public void onInitNaviSuccess() {
//        //目前采用驾车路径规划的计算策略10(速度优先+躲避拥堵+距离较短）),以前用的策略2(单路径-距离优先（距离最短）)
//        mAMapNavi.calculateDriveRoute(endList, null, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);
    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onGetNavigationText(String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }


    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

    @Override
    public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {

    }

    @Override
    public void hideModeCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {
        Log.e("导航~~~~~~~~~~", "控件初始化完成1");
        mAMapNavi.startNavi(NaviType.GPS);//驾车导航
    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }

    @Override
    public void onCalculateRouteSuccess(AMapCalcRouteResult aMapCalcRouteResult) {
        Log.e("导航~~~~~~~~~~", "控件初始化完成2");
        mAMapNavi.startNavi(NaviType.GPS);//驾车导航
    }

    @Override
    public void onCalculateRouteFailure(AMapCalcRouteResult aMapCalcRouteResult) {

    }

    @Override
    public void onNaviRouteNotify(AMapNaviRouteNotifyData aMapNaviRouteNotifyData) {

    }

    @Override
    public void onGpsSignalWeak(boolean b) {

    }

    @Override
    public void onNaviSetting() {
    }

    @Override
    public void onNaviCancel() {
        //todo 通知flutter关闭页面
        Log.e("导航~~~~~~~~~~", "点击了导航视图里面得退出导航按钮");
        mAMapNavi.destroy();
        mChannel.invokeMethod("close", null);
    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {

    }

    @Override
    public void onNaviViewLoaded() {

    }

    @Override
    public void onMapTypeChanged(int i) {

    }

    @Override
    public void onNaviViewShowMode(int i) {

    }
}
